//
//  GiveTaskViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/9.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
class GiveTaskViewController: BaseViewController {

    @IBOutlet var carMileageLabel: UILabel!
    
    @IBOutlet var taskTypeLabel: UILabel!
    @IBOutlet var userNameLabel: UILabel!
    @IBOutlet var priceLabel: UILabel!
    
    @IBOutlet var contentLabel: UILabel!
    
    @IBOutlet var userHeaderImageView: UIImageView!
    
    @IBOutlet var phoneLabel: UILabel!
    
    @IBOutlet var appointMentTimeLabel: UILabel!
    
    @IBOutlet var carNumLabel: UILabel!
    
    
    @IBOutlet var carModelLabel: UILabel!
    
    @IBOutlet var priceTF: UITextField!
    
    @IBOutlet var taskContentTF: UITextField!
    
    @IBOutlet var sumbitBtn: UIButton!
    
    @IBOutlet var locationBtn: UIButton!
    
    var taskModel:PlatformTaskModel?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        updateVcEvents()

    }
    
    func updateVcEvents() -> Void {
        sumbitBtn.roundCorner(radius: 5);
        userHeaderImageView.roundCorner(radius: userHeaderImageView.frame.height/2)
        taskTypeLabel.roundCorner(radius: 3)
        taskTypeLabel.layer.borderWidth = 1
        taskTypeLabel.layer.borderColor = UIColor.HexColor(0x727272).cgColor        
        priceLabel.text = "预算:¥" + (taskModel?.budget_money)! + "元"
        userHeaderImageView.sd_setImage(with: (NSURL(string:(taskModel?.shead_url)!)! as URL), placeholderImage: UIImage.init(named: "defultHeaderImg"))
        contentLabel.text = taskModel?.describe
        userNameLabel.text = taskModel?.sname
        taskTypeLabel.text = taskModel?.name
        
        phoneLabel.text = "电话:" + replaceStrForPhone(str: (taskModel?.phone)!)
        appointMentTimeLabel.text = "预约时间:" + (taskModel?.subscribe_time)!
        carNumLabel.text = "车牌号:" + (taskModel?.num)!
        carModelLabel.text = "车型:" + (taskModel?.car_model)!
        carMileageLabel.text = "行驶里程:" + (taskModel?.mileage)! + "KM"
        locationBtn.setTitle(taskModel?.address, for: UIControlState.normal)
    }
    
    @IBAction func sumbitBtnPress(_ sender: UIButton) {
        if priceTF.text! == "" || taskContentTF.text! == ""{
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再发布", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        requestGivePlatformTask(taskId: (taskModel?.tid)!, price: priceTF.text!, plan: taskContentTF.text!)
    }
    
    @IBAction func lookLocationBtnPress(_ sender: UIButton) {
        
        let lookLocationVC = LookLocationViewController()
        lookLocationVC.taskModel = taskModel;    self.navigationController?.pushViewController(lookLocationVC, animated: true)
    }
    
    //网络请求
    //给方案
    func requestGivePlatformTask(taskId:String,price:String,plan:String) -> Void {
        let param =
            ["task_id":taskId,
             "offer":price,
             "plan":plan,
             "flag":"2",//区别用户端和商户端 1用户端  2 商户端
                "uid":UserModel.shareUser.data?.uid ?? String(),
                ] as [String : Any]
        
        Alamofire.request(givePlanUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        hud?.mode = MBProgressHUDMode.text
                        hud?.labelText = "给出方案成功"
                        hud?.show(animated: true, whileExecuting: {                        Thread.sleep(forTimeInterval: 0.8)
                        }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                        })
                    case false:
                        MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }

    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
